package com.ruoyi.framework.handler;


import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class MyMetaObjectHandler implements MetaObjectHandler {


    @Override
    public void insertFill(MetaObject metaObject) {
        System.out.println("插入执行了");
        this.strictInsertFill(metaObject, "createBy", String.class, SecurityUtils.getUsername());
        this.strictInsertFill(metaObject, "createTime", Date.class, DateUtils.getNowDate()); // 创建时间
        this.strictInsertFill(metaObject,"deleteFlag", String.class,"0");

    }

    @Override
    public void updateFill(MetaObject metaObject) {
        System.out.println("修改执行了");
        this.strictUpdateFill(metaObject, "updateTime", Date.class, DateUtils.getNowDate());
        this.strictUpdateFill(metaObject, "updateBy", String.class, SecurityUtils.getUsername());
    }

    private void fillValue(String fieldName, Object data, MetaObject metaObject) {
        if (metaObject.hasSetter(fieldName)) {
            // 值为空时设置默认值
            Object sidObj = getFieldValByName(fieldName, metaObject);
            if (sidObj == null || "updateBy".equals(fieldName) || "updateTime".equals(fieldName)) {
                setFieldValByName(fieldName, data, metaObject);
            }
        }
    }


}